from db import db1

class User_Role:
    def __init__(self):
        pass
    # 添加权限
    def set_user_role(self,uid,rolelist):
        for j in rolelist:
            for i in rolelist:
                sql = "select role_id2 from role_reject where role_id1=%d " % (int(i))
                res = db1.find(sql)
                if res:
                    if int(j) != int(res['role_id2']):
                        sql = "insert into user_role values(%d,%d)" % (int(uid), int(j))
                        db1.update(sql)
    # 修改权限
    def update_user_role(self,uid,rolelist):
        sql = "delete from user_role where user_id=%d "%(int(uid))
        db1.update(sql)
        self.set_user_role(uid,rolelist)

    # 删除单个权限
    def delete_user_role(self,user_id,role_id):
        sql = "delete from user_role where user_id=%d and role_id=%d"%(int(user_id),int(role_id))
        db1.update(sql)




user_role = User_Role()